import com.google.common.base.Charsets;
import com.google.common.io.CharSink;
import com.google.common.io.Files;
import lombok.Data;
import one.util.streamex.StreamEx;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * TaskJob
 * </p>
 *
 * @author: lianjun
 * @since: 2022-02-16
 */
@Data
public class TaskJob{

    public synchronized void gensql(int start, int threadIndex) {
        System.out.println("start "+start +" index "+threadIndex);
        Long storeId = 3600001000001L;
        String storeName = "泸州老窖（高新园店）";
        Long spuId = 9L;
        Long skuId = 201L;
        String pic = "https://pub2priv2.wljiam.com/woss/view/public/company?p=3600001000000/mall/3788f132a52b49a2a442e12131994a65-ORIGIN.png&fileType=image/png&fileMeta=725*1000#16";
        String productName = "压2压-1";
        String attr = "{\"sp1\":\"红-#F41010\",\"sp2\":\"S\",\"sp3\":\"27\",\"sp4\":\"纯色\",\"sp5\":\"印花\"}";

        String insertStr =
                """
                INSERT INTO t_customer_cart (`id`, `store_id`, `user_id`, `store_name`, `product_id`, `product_sku_id`, `quantity`, `price`, `current_price`, `product_pic`, `product_name`, `product_sub_title`, `showed`, `product_attr`, `create_time`, `update_time`, `deleted`) VALUES
                """;

        String value =
                """
                 (%d, %d, %d, '%s', %d, %d, 1, 1000.00, 1000.00, '%s', '%s', NULL, 0, '%s', '2022-02-16 11:14:56', '2022-02-16 11:14:56', 0),
                """;

        String value1 =
                """
                 (%d, %d, %d, '%s', %d, %d, 1, 1000.00, 1000.00, '%s', '%s', NULL, 0, '%s', '2022-02-16 11:14:56', '2022-02-16 11:14:56', 0);
                """;

        // 总数量
        int total = 100000;
        // 批量插入数量
        int batchInsertCount = 100;
        //单个sql文件数量
        int fileInsertCount = 500;
        var lines = new ArrayList<String>();
        StringBuilder sb = null;
        var longIdGenerator = new LongIdGenerator(100L + threadIndex);
        var ids = new HashSet<Long>();
        for(int i=start+1;i<=start+total;i++){
            Long userId = 1000000000000000000L;
            Long id = longIdGenerator.generate();
            ids.add(id);
            userId = userId + i;
            if(i % batchInsertCount==0){
                String sf = String.format(value1,id,storeId,userId,storeName,spuId,skuId,pic,productName,attr);
                sb.append(sf).append("\r");
                lines.add(insertStr+"\r"+ sb);
                sb = null;
            }else{
                if(sb == null){
                    sb = new StringBuilder();
                }
                String sf = String.format(value,id,storeId,userId,storeName,spuId,skuId,pic,productName,attr);
                sb.append(sf).append("\r");
            }
            System.out.println("i==>"+i+" thread "+Thread.currentThread().getName());
        }

        if(ids.size() != total){
            System.out.println("ids error");
        }

        write(fileInsertCount, lines,threadIndex);
        //System.out.println(lines);
        System.out.println("end "+start +" index "+threadIndex);
    }

    private synchronized void write(int fileInsertCount, ArrayList<String> lines,int threadIndex) {
        var sublines = StreamEx.ofSubLists(lines, fileInsertCount).collect(Collectors.toList());
        int index = 0;
        var path = "c:/Users/Administrator/Desktop/cart/t_customer_cart_%d_%d.sql";
        for (List<String> list : sublines) {
            String format = String.format(path,threadIndex,index++);
            File file = new File(format);
            CharSink sink = Files.asCharSink(file, Charsets.UTF_8);
            try {
                sink.writeLines(list, " ");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
